package com.zyx.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @Description: 统计发送消息的成功和失败数量
 * @Author: ZYX
 * @Date: 2022/1/13 14:14
 * @Version: 1.0
 */
public class CountInterceptor implements ProducerInterceptor<String,String> {

    private Integer successed = 0;
    private Integer failed = 0;

    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> producerRecord) {
        return producerRecord;
    }

    @Override
    public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {
        if (e != null){
            // 失败
            failed++;
        }else {
            // 成功
            successed++;
        }
    }

    @Override
    public void close() {
        System.out.println("successed====>"+successed);
        System.out.println("failed=======>"+failed);
    }

    @Override
    public void configure(Map<String, ?> map) {

    }
}
